package com.tinyseed.emcp.api.maintenance.rest;

import com.tinyseed.emcp.api.exception.entity.ExceptionInfo;
import com.tinyseed.emcp.api.exception.service.ExceptionInfoService;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfo;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfoDTO;
import com.tinyseed.emcp.api.maintenance.domain.MaintenanceInfoMapper;
import com.tinyseed.emcp.api.maintenance.domain.query.MaintenanceInfoQueryCriteria;
import com.tinyseed.emcp.api.maintenance.service.MaintenanceService;
import com.tinyseed.hmxx.admin.system.domain.User;
import com.tinyseed.hmxx.common.base.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api/maintenanceInfo")
public class MaintenanceController extends BaseController {

    @Autowired
    private MaintenanceService maintenanceService;

    @Autowired
    private MaintenanceInfoMapper maintenanceInfoMapper;

    @Autowired
    private ExceptionInfoService exceptionInfoService;


    @ApiOperation("新增维保单")
    @PostMapping
    public ResponseEntity create(@RequestBody MaintenanceInfoDTO resources) {
        String name = getUserInfo();
        log.info(name);
        //新增维保单,修改异常状态
        ExceptionInfo exceptionInfo = exceptionInfoService.findByid(resources.getExceptionInfo().getId());

        maintenanceService.addTelReturnRecord(resources);

        if(resources.getShouldMaintenance().equals("true")){
            exceptionInfoService.createMTInfoByExceptionInfo(exceptionInfo,maintenanceInfoMapper.toEntity(resources));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("新增回访记录")
    @PutMapping("/addTelReturnRecord")
    public ResponseEntity addTelReturnRecord(@RequestBody MaintenanceInfoDTO resources) {

        return new ResponseEntity<>(HttpStatus.OK);
    }
    @ApiOperation("查看回访记录")
    @GetMapping("/getTelReturnRecord")
    @ResponseBody
    public ResponseEntity getTelReturnRecord(int limit, int page) {
        Pageable pageable = PageRequest.of(page - 1 , limit);

        return new ResponseEntity<>( maintenanceService.getTelReturnRecord(pageable),HttpStatus.OK);
    }

    @ApiOperation("审核下发")
    @PutMapping("/issuedAndAudit")
    public ResponseEntity issuedAndAudit(@RequestBody MaintenanceInfoDTO resources) {
        String name = getUserInfo();
        log.info(name);
        MaintenanceInfo mt = maintenanceInfoMapper.toEntity(resources);

        //新增维保单,修改异常状态
        maintenanceService.auditedMT(mt);
        maintenanceService.issuedMT(mt);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("维保人员确认维保内容")
    @PutMapping("/confirm")
    public ResponseEntity confirm(@RequestBody MaintenanceInfoDTO resources) {
        String name = getUserInfo();
        log.info(name);
        MaintenanceInfo mt = maintenanceInfoMapper.toEntity(resources);

        maintenanceService.confirmMT(mt);

        return new ResponseEntity<>(HttpStatus.OK);
    }


    @ApiOperation("维保人员提交维保结果")
    @PutMapping("/submit")
    public ResponseEntity submit(@RequestBody MaintenanceInfoDTO resources) {
        String name = getUserInfo();
        log.info(name);
        MaintenanceInfo mt = maintenanceInfoMapper.toEntity(resources);

        maintenanceService.submit(mt);

        return new ResponseEntity<>(HttpStatus.OK);
    }





    @ApiOperation("审核人员审核维保结果")
    @PutMapping("/complete")
    public ResponseEntity complete(@RequestBody MaintenanceInfoDTO resources) {
        String name = getUserInfo();
        log.info(name);
        MaintenanceInfo mt = maintenanceInfoMapper.toEntity(resources);

        maintenanceService.complete(mt);

        return new ResponseEntity<>(HttpStatus.OK);
    }


    @ApiOperation("查询维保列表")
    @GetMapping
    public ResponseEntity getMaintenanceInfo(MaintenanceInfoQueryCriteria criteria, Pageable pageable) {
       return new ResponseEntity<>( maintenanceService.queryAll(criteria,pageable),HttpStatus.OK);
    }





}
